package javax.per.toolkit.tools.msg;

import java.io.IOException;
import java.net.URL;
import java.util.Locale;

import javax.per.toolkit.file.FileUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Msg {
	
	private static String prefix="";
	
	static{
		prefix=getLocalePrefix();
		//Log.getLogger().debug(prefix);
	}
	
	public static String get(String key){
		String content = null;
		try {
			URL url=Msg.class.getResource(prefix+"msg.xml");
			if(url==null)
				url=Msg.class.getResource("msg.xml");
			content = FileUtil.getContent(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Document doc=Jsoup.parse(content);
		Element root=doc.getElementById("root");
		Elements eles=root.children();
		for(Element ele:eles){
			if(ele.attr("key").trim().equals(key.trim())){
				return ele.attr("value");
			}
		}
		return null;
	}
	
	
	private static String getLocalePrefix(){
		return Locale.getDefault().getLanguage();
	}
	
	public static void setLocalePrefix(String pre){
		prefix=pre;
	}
	
	
}
